

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/icon/dw.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Memory">
  <meta name="keywords" content="">
  
    <meta name="description" content="🍜 界面之美，框架之魂：如何用 Vue CLI、Acro Design 和 Open API 创造前端新纪元">
<meta property="og:type" content="article">
<meta property="og:title" content="破茧成蝶：从零开始构建独具匠心前端项目框架">
<meta property="og:url" content="http://example.com/2024/01/17/%E7%A0%B4%E8%8C%A7%E6%88%90%E8%9D%B6%EF%BC%9A%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E6%9E%84%E5%BB%BA%E7%8B%AC%E5%85%B7%E5%8C%A0%E5%BF%83%E5%89%8D%E7%AB%AF%E9%A1%B9%E7%9B%AE%E6%A1%86%E6%9E%B6/index.html">
<meta property="og:site_name" content="Memory&#39;s blog">
<meta property="og:description" content="🍜 界面之美，框架之魂：如何用 Vue CLI、Acro Design 和 Open API 创造前端新纪元">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/img/artical/sky3.jpg">
<meta property="article:published_time" content="2024-01-17T13:11:03.000Z">
<meta property="article:modified_time" content="2024-05-06T13:11:03.000Z">
<meta property="article:author" content="Memory">
<meta property="article:tag" content="经验">
<meta property="article:tag" content="Vue CLI">
<meta property="article:tag" content="Acro Design 组件库">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://example.com/img/artical/sky3.jpg">
  
  
  
  <title>破茧成蝶：从零开始构建独具匠心前端项目框架 - Memory&#39;s blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":100,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":false,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  

  

  

  

  

  

  

  



  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 75vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Memory&#39;s blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                <span>友链</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/newBG/girl.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="破茧成蝶：从零开始构建独具匠心前端项目框架"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        Memory
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2024-01-17 21:11" pubdate>
          2024年1月17日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7.4k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          19 分钟
        
      </span>
    

    
    
      
        <span id="leancloud-page-views-container" class="post-meta" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="leancloud-page-views"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar category-bar" style="margin-right: -1rem">
    





<div class="category-list">
  
  
    
    
    
    <div class="category row nomargin-x">
      <a class="category-item 
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="Vue CLI"
        id="heading-ddfef4ff57fe636111bb8fa6714c70ce" role="tab" data-toggle="collapse" href="#collapse-ddfef4ff57fe636111bb8fa6714c70ce"
        aria-expanded="true"
      >
        Vue CLI
        <span class="list-group-count">(1)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse show" id="collapse-ddfef4ff57fe636111bb8fa6714c70ce"
           role="tabpanel" aria-labelledby="heading-ddfef4ff57fe636111bb8fa6714c70ce">
        
        
          
          
  <div class="category-post-list">
    
    
  </div>

          
  
    
    
    
    <div class="category-sub row nomargin-x">
      <a class="category-subitem 
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="Acro Design 组件库"
        id="heading-b8457c1e3c902228ac23586bf0b1996f" role="tab" data-toggle="collapse" href="#collapse-b8457c1e3c902228ac23586bf0b1996f"
        aria-expanded="true"
      >
        Acro Design 组件库
        <span class="list-group-count">(1)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse show" id="collapse-b8457c1e3c902228ac23586bf0b1996f"
           role="tabpanel" aria-labelledby="heading-b8457c1e3c902228ac23586bf0b1996f">
        
        
          
          
  <div class="category-post-list">
    
    
  </div>

          
  
    
    
    
    <div class="category-sub row nomargin-x">
      <a class="category-subitem 
          list-group-item category-item-action col-10 col-md-11 col-xm-11" title="经验"
        id="heading-32a8e000e928e6dc255876d9fc0178b3" role="tab" data-toggle="collapse" href="#collapse-32a8e000e928e6dc255876d9fc0178b3"
        aria-expanded="true"
      >
        经验
        <span class="list-group-count">(1)</span>
        <i class="iconfont icon-arrowright"></i>
      </a>
      
      <div class="category-collapse collapse show" id="collapse-32a8e000e928e6dc255876d9fc0178b3"
           role="tabpanel" aria-labelledby="heading-32a8e000e928e6dc255876d9fc0178b3">
        
        
          
  <div class="category-post-list">
    
    
      
      
        <a href="/2024/01/17/%E7%A0%B4%E8%8C%A7%E6%88%90%E8%9D%B6%EF%BC%9A%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E6%9E%84%E5%BB%BA%E7%8B%AC%E5%85%B7%E5%8C%A0%E5%BF%83%E5%89%8D%E7%AB%AF%E9%A1%B9%E7%9B%AE%E6%A1%86%E6%9E%B6/" title="破茧成蝶：从零开始构建独具匠心前端项目框架"
           class="list-group-item list-group-item-action
           active">
          <span class="category-post">破茧成蝶：从零开始构建独具匠心前端项目框架</span>
        </a>
      
    
  </div>

        
      </div>
    </div>
  
        
      </div>
    </div>
  
        
      </div>
    </div>
  
</div>


  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">破茧成蝶：从零开始构建独具匠心前端项目框架</h1>
            
              <p class="note note-success">
                
                  
                    本文最后更新于：2 个月前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h2 id="框架构建"><a href="#框架构建" class="headerlink" title="框架构建"></a>框架构建</h2><p><a target="_blank" rel="noopener" href="https://cli.vuejs.org/zh/guide/installation.html">安装 | Vue CLI (vuejs.org)</a></p>
<p><a target="_blank" rel="noopener" href="https://arco.design/vue/docs/start">Arco Design Vue</a></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;template&gt;<br>  &lt;BasicLayout /&gt;<br>&lt;/template&gt;<br><br>&lt;style&gt;&lt;/style&gt;<br>&lt;script&gt;<br>import BasicLayout from &quot;@/layouts/BasicLayout&quot;;<br><br>export default &#123;<br>  components: &#123; BasicLayout &#125;,<br>&#125;;<br>&lt;/script&gt;<br><br></code></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;template&gt;<br>  &lt;GlobalHeader /&gt;<br>&lt;/template&gt;<br><br>&lt;script&gt;<br>import GlobalHeader from &quot;@/components/GlobalHeader&quot;;<br><br>export default &#123;<br>  name: &quot;BasicLayout&quot;,<br>  components: &#123; GlobalHeader &#125;,<br>&#125;;<br>&lt;/script&gt;<br><br>&lt;style scoped&gt;&lt;/style&gt;<br><br></code></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;template&gt;<br>  &lt;div&gt;&lt;/div&gt;<br>&lt;/template&gt;<br><br>&lt;script&gt;<br>export default &#123;<br>  name: &quot;GlobalHeader&quot;,<br>&#125;;<br>&lt;/script&gt;<br><br>&lt;style scoped&gt;&lt;/style&gt;<br><br></code></pre></td></tr></table></figure>

<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20240505172201305.png" alt="image-20240505172201305"></p>
<h2 id="路由跳转"><a href="#路由跳转" class="headerlink" title="路由跳转"></a>路由跳转</h2><p>router 下的 index.ts：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">import</span> &#123; createRouter, createWebHistory &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;vue-router&quot;</span>;<br><span class="hljs-keyword">import</span> &#123; routes &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;@/router/routes&quot;</span>;<br><br><span class="hljs-keyword">const</span> router = <span class="hljs-title function_">createRouter</span>(&#123;<br>  <span class="hljs-attr">history</span>: <span class="hljs-title function_">createWebHistory</span>(process.<span class="hljs-property">env</span>.<span class="hljs-property">BASE_URL</span>),<br>  routes,<br>&#125;);<br><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> router;<br><br></code></pre></td></tr></table></figure>

<p>router 下的 router.ts：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">import</span> &#123; <span class="hljs-title class_">RouteRecordRaw</span> &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;vue-router&quot;</span>;<br><span class="hljs-keyword">import</span> <span class="hljs-title class_">HomeView</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;@/views/HomeView.vue&quot;</span>;<br><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> <span class="hljs-attr">routes</span>: <span class="hljs-title class_">Array</span>&lt;<span class="hljs-title class_">RouteRecordRaw</span>&gt; = [<br>  &#123;<br>    <span class="hljs-attr">path</span>: <span class="hljs-string">&quot;/&quot;</span>,<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&quot;首页&quot;</span>,<br>    <span class="hljs-attr">component</span>: <span class="hljs-title class_">HomeView</span>,<br>  &#125;,<br>  &#123;<br>    <span class="hljs-attr">path</span>: <span class="hljs-string">&quot;/about&quot;</span>,<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&quot;关于&quot;</span>,<br>    <span class="hljs-comment">// route level code-splitting</span><br>    <span class="hljs-comment">// this generates a separate chunk (about.[hash].js) for this route</span><br>    <span class="hljs-comment">// which is lazy-loaded when the route is visited.</span><br>    <span class="hljs-attr">component</span>: <span class="hljs-function">() =&gt;</span><br>      <span class="hljs-keyword">import</span>(<span class="hljs-comment">/* webpackChunkName: &quot;about&quot; */</span> <span class="hljs-string">&quot;../views/AboutView.vue&quot;</span>),<br>  &#125;,<br>];<br><br></code></pre></td></tr></table></figure>

<p>动态导航栏：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;a-menu<br>     mode=&quot;horizontal&quot;<br>     :selected-keys=&quot;selectedKeys&quot;<br>     @menu-item-click=&quot;doMenuClick&quot;<br>   &gt;<br>     &lt;a-menu-item<br>       key=&quot;0&quot;<br>       :style=&quot;&#123; padding: 0, marginRight: &#x27;38px&#x27; &#125;&quot;<br>       disabled<br>     &gt;<br>       &lt;div class=&quot;title-bar&quot;&gt;<br>         &lt;img class=&quot;logo&quot; src=&quot;../assets/oj-logo.svg&quot; /&gt;<br>         &lt;div class=&quot;title&quot;&gt;OJ&lt;/div&gt;<br>       &lt;/div&gt;<br>     &lt;/a-menu-item&gt;<br>     &lt;a-menu-item v-for=&quot;item in routes&quot; :key=&quot;item.path&quot;&gt;<br>       &#123;&#123; item.name &#125;&#125;<br>     &lt;/a-menu-item&gt;<br>   &lt;/a-menu&gt;<br></code></pre></td></tr></table></figure>

<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">import</span> &#123; ref &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;vue&quot;</span>;<br><span class="hljs-keyword">import</span> &#123; useRouter &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;vue-router&quot;</span>;<br><span class="hljs-keyword">import</span> &#123; routes &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;@/router/routes&quot;</span>;<br><br><span class="hljs-keyword">const</span> router = <span class="hljs-title function_">useRouter</span>();<br><span class="hljs-comment">// 默认主页</span><br><span class="hljs-keyword">const</span> selectedKeys = <span class="hljs-title function_">ref</span>([<span class="hljs-string">&quot;/&quot;</span>]);<br><br><span class="hljs-comment">// 路由跳转后，更新选中的菜单项</span><br>router.<span class="hljs-title function_">afterEach</span>(<span class="hljs-function">(<span class="hljs-params">to, <span class="hljs-keyword">from</span>, failure</span>) =&gt;</span> &#123;<br>  selectedKeys.<span class="hljs-property">value</span> = [to.<span class="hljs-property">path</span>];<br>&#125;);<br><br><span class="hljs-comment">// 跳转路由</span><br><span class="hljs-keyword">const</span> <span class="hljs-title function_">doMenuClick</span> = (<span class="hljs-params">key: <span class="hljs-built_in">string</span></span>) =&gt; &#123;<br>  router.<span class="hljs-title function_">push</span>(&#123;<br>    <span class="hljs-attr">path</span>: key,<br>  &#125;);<br>&#125;;<br></code></pre></td></tr></table></figure>

<p><strong>效果如下：</strong></p>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20240505180151544.png" alt="image-20240505180151544"></p>
<h2 id="全局状态管理"><a href="#全局状态管理" class="headerlink" title="全局状态管理"></a>全局状态管理</h2><p><a target="_blank" rel="noopener" href="https://github.com/vuejs/vuex/blob/main/examples/classic/shopping-cart/store/index.js">vuex/examples/classic/shopping-cart/store/index.js at main · vuejs/vuex (github.com)</a></p>
<p><a target="_blank" rel="noopener" href="https://vuex.vuejs.org/zh/guide/#%E6%9C%80%E7%AE%80%E5%8D%95%E7%9A%84-store">开始 | Vuex (vuejs.org)</a></p>
<p>store 下的 user.ts</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// initial state</span><br><span class="hljs-keyword">import</span> &#123; <span class="hljs-title class_">StoreOptions</span> &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;vuex&quot;</span>;<br><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;<br>  <span class="hljs-attr">namespaced</span>: <span class="hljs-literal">true</span>,<br>  <span class="hljs-attr">state</span>: <span class="hljs-function">() =&gt;</span> (&#123;<br>    <span class="hljs-attr">loginUser</span>: &#123;<br>      <span class="hljs-attr">userName</span>: <span class="hljs-string">&quot;未登录&quot;</span>,<br>    &#125;,<br>  &#125;),<br>  <span class="hljs-attr">actions</span>: &#123;<br>    <span class="hljs-keyword">async</span> <span class="hljs-title function_">getLoginUser</span>(<span class="hljs-params">&#123; commit, state &#125;, payload</span>) &#123;<br>      <span class="hljs-title function_">commit</span>(<span class="hljs-string">&quot;updateUser&quot;</span>, &#123; <span class="hljs-attr">userName</span>: <span class="hljs-string">&quot;memory&quot;</span> &#125;);<br>    &#125;,<br>  &#125;,<br>  <span class="hljs-attr">mutations</span>: &#123;<br>    <span class="hljs-title function_">updateUser</span>(<span class="hljs-params">state, payload</span>) &#123;<br>      state.<span class="hljs-property">loginUser</span> = payload;<br>    &#125;,<br>  &#125;,<br>&#125; <span class="hljs-keyword">as</span> <span class="hljs-title class_">StoreOptions</span>&lt;<span class="hljs-built_in">any</span>&gt;;<br><br></code></pre></td></tr></table></figure>

<p>index.ts</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">import</span> &#123; createStore &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;vuex&quot;</span>;<br><span class="hljs-keyword">import</span> user <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;@/store/user&quot;</span>;<br><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-title function_">createStore</span>(&#123;<br>  <span class="hljs-attr">state</span>: &#123;&#125;,<br>  <span class="hljs-attr">getters</span>: &#123;&#125;,<br>  <span class="hljs-attr">mutations</span>: &#123;&#125;,<br>  <span class="hljs-attr">actions</span>: &#123;&#125;,<br>  <span class="hljs-attr">modules</span>: &#123;<br>    user,<br>  &#125;,<br>&#125;);<br><br></code></pre></td></tr></table></figure>

<p>main.ts</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">import</span> store <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;./store&quot;</span>;<br><br><span class="hljs-title function_">createApp</span>(<span class="hljs-title class_">App</span>).<span class="hljs-title function_">use</span>(<span class="hljs-title class_">ArcoVue</span>).<span class="hljs-title function_">use</span>(store).<span class="hljs-title function_">use</span>(router).<span class="hljs-title function_">mount</span>(<span class="hljs-string">&quot;#app&quot;</span>);<br><br></code></pre></td></tr></table></figure>

<p><strong>store 原理（执行流程）</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 使用 store</span><br><span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>  store.<span class="hljs-title function_">dispatch</span>(<span class="hljs-string">&quot;user/getLoginUser&quot;</span>, &#123;<br>    <span class="hljs-attr">userName</span>: <span class="hljs-string">&quot;回忆如初&quot;</span>,<br>  &#125;);<br>&#125;, <span class="hljs-number">3000</span>);<br></code></pre></td></tr></table></figure>

<p>这样引入 store，执行 dispatch方法，根据 actions 下的路径，提供参数，执行 mutations 下的方法。</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-attr">actions</span>: &#123;<br>    <span class="hljs-keyword">async</span> <span class="hljs-title function_">getLoginUser</span>(<span class="hljs-params">&#123; commit, state &#125;, payload</span>) &#123;<br>      <span class="hljs-title function_">commit</span>(<span class="hljs-string">&quot;updateUser&quot;</span>, payload);<br>    &#125;,<br>  &#125;,<br></code></pre></td></tr></table></figure>

<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-attr">mutations</span>: &#123;<br>   <span class="hljs-title function_">updateUser</span>(<span class="hljs-params">state, payload</span>) &#123;<br>     state.<span class="hljs-property">loginUser</span> = payload;<br>   &#125;,<br> &#125;,<br></code></pre></td></tr></table></figure>

<p>mutations 改变了 state 的值，根据传入的参数改变了。</p>
<p>我们尝试在页面获取 store 值，并展示：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;a-col flex=&quot;100px&quot;&gt;<br>    &lt;div&gt;<br>      &#123;&#123; store.state.user?.loginUser?.userName ?? &quot;未登录&quot; &#125;&#125;<br>    &lt;/div&gt;<br>  &lt;/a-col&gt;<br></code></pre></td></tr></table></figure>

<p>效果如下：</p>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20240505182741049.png" alt="image-20240505182741049"></p>
<p>另外，action 这里可以写死，不接受参数：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-attr">actions</span>: &#123;<br>    <span class="hljs-keyword">async</span> <span class="hljs-title function_">getLoginUser</span>(<span class="hljs-params">&#123; commit, state &#125;, payload</span>) &#123;<br>      <span class="hljs-title function_">commit</span>(<span class="hljs-string">&quot;updateUser&quot;</span>, &#123; <span class="hljs-attr">userName</span>: <span class="hljs-string">&quot;memory&quot;</span> &#125;);<br>    &#125;,<br>  &#125;,<br></code></pre></td></tr></table></figure>

<h2 id="全局权限管理"><a href="#全局权限管理" class="headerlink" title="全局权限管理"></a>全局权限管理</h2><p>关键在于这段逻辑，App.vue 下：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">import</span> router <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;@/router&quot;</span>;<br><span class="hljs-keyword">import</span> store <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;@/store&quot;</span>;<br><br>router.<span class="hljs-title function_">beforeEach</span>(<span class="hljs-function">(<span class="hljs-params">to, <span class="hljs-keyword">from</span>, next</span>) =&gt;</span> &#123;<br>  <span class="hljs-comment">// 仅管理员可见，判断当前用户是否有权限</span><br>  <span class="hljs-keyword">if</span> (to.<span class="hljs-property">meta</span>?.<span class="hljs-property">access</span> === <span class="hljs-string">&quot;canAdmin&quot;</span>) &#123;<br>    <span class="hljs-keyword">if</span> (store.<span class="hljs-property">state</span>.<span class="hljs-property">user</span>?.<span class="hljs-property">loginUser</span>?.<span class="hljs-property">role</span> !== <span class="hljs-string">&quot;admin&quot;</span>) &#123;<br>      <span class="hljs-title function_">next</span>(<span class="hljs-string">&quot;/noAuth&quot;</span>);<br>      <span class="hljs-keyword">return</span>;<br>    &#125;<br>  &#125;<br>  <span class="hljs-title function_">next</span>();<br>&#125;);<br><br></code></pre></td></tr></table></figure>

<p>设置路由访问权限，必须为管理员可见：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs ts">&#123;<br>    <span class="hljs-attr">path</span>: <span class="hljs-string">&quot;/auth&quot;</span>,<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&quot;管理员可见&quot;</span>,<br>    <span class="hljs-attr">component</span>: <span class="hljs-title class_">AuthView</span>,<br>    <span class="hljs-attr">meta</span>: &#123;<br>      <span class="hljs-attr">access</span>: <span class="hljs-string">&quot;canAdmin&quot;</span>,<br>    &#125;,<br>  &#125;,<br></code></pre></td></tr></table></figure>

<p>默认用户权限，测试用：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-attr">state</span>: <span class="hljs-function">() =&gt;</span> (&#123;<br>  <span class="hljs-attr">loginUser</span>: &#123;<br>    <span class="hljs-attr">userName</span>: <span class="hljs-string">&quot;未登录&quot;</span>,<br>    <span class="hljs-attr">role</span>: <span class="hljs-string">&quot;admin&quot;</span>,<br>  &#125;,<br>&#125;),<br></code></pre></td></tr></table></figure>

<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20240505190305372.png" alt="image-20240505190305372"></p>
<p>我们发现能正常跳转页面，但这样做：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// 使用 store</span><br><span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>  store.<span class="hljs-title function_">dispatch</span>(<span class="hljs-string">&quot;user/getLoginUser&quot;</span>, &#123;<br>    <span class="hljs-attr">userName</span>: <span class="hljs-string">&quot;回忆如初&quot;</span>,<br>    <span class="hljs-attr">role</span>: <span class="hljs-string">&quot;noAdmin&quot;</span>,<br>  &#125;);<br>&#125;, <span class="hljs-number">3000</span>);<br></code></pre></td></tr></table></figure>

<p>三秒过后，该用户不是管理员权限，访问一个管理员可见的页面，直接重定向：</p>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20240505190310211.png" alt="image-20240505190310211"></p>
<h2 id="隐藏菜单"><a href="#隐藏菜单" class="headerlink" title="隐藏菜单"></a>隐藏菜单</h2><p>构造通用的导航栏组件，根据配置控制菜单栏的显隐</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs ts">&#123;<br>    <span class="hljs-attr">path</span>: <span class="hljs-string">&quot;/hide&quot;</span>,<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&quot;隐藏页面&quot;</span>,<br>    <span class="hljs-attr">component</span>: noAuthView,<br>    <span class="hljs-attr">meta</span>: &#123;<br>      <span class="hljs-attr">hideInMenu</span>: <span class="hljs-literal">true</span>,<br>    &#125;,<br>  &#125;,<br></code></pre></td></tr></table></figure>

<p>过滤，仅展示显示在菜单上的路由数组</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;a-menu-item v-for=&quot;item in visibleRoutes&quot; :key=&quot;item.path&quot;&gt;<br>         &#123;&#123; item.name &#125;&#125;<br>       &lt;/a-menu-item&gt;<br></code></pre></td></tr></table></figure>

<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">import</span> &#123; routes &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;@/router/routes&quot;</span>;<br><br><span class="hljs-comment">// 展示在菜单上的路由数组</span><br><span class="hljs-keyword">const</span> visibleRoutes = routes.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">item, index</span>) =&gt;</span> &#123;<br>  <span class="hljs-keyword">return</span> !item.<span class="hljs-property">meta</span>?.<span class="hljs-property">hideInmenu</span>;<br>&#125;);<br></code></pre></td></tr></table></figure>

<p>除了 <strong>根据配置权限隐藏菜单</strong>，还需要根据用户权限，只有具有相关权限的用户，才能看到该菜单。</p>
<p>检测用户权限：</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">import</span> <span class="hljs-variable constant_">ACCESS_ENUM</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;@/access/accessEnum&quot;</span>;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 检查权限（判断当前登录用户是否具有某个权限）</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> loginUser 当前登录用户</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@param</span> needAccess 需要有的权限</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@return</span> boolean 有无权限</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">const</span> <span class="hljs-title function_">checkAccess</span> = (<span class="hljs-params">loginUser: <span class="hljs-built_in">any</span>, needAccess = ACCESS_ENUM.NOT_LOGIN</span>) =&gt; &#123;<br>  <span class="hljs-comment">// 获取当前登录用户具有的权限（如果没有 loginUser，则表示未登录）</span><br>  <span class="hljs-keyword">const</span> loginUserAccess = loginUser?.<span class="hljs-property">userRole</span> ?? <span class="hljs-variable constant_">ACCESS_ENUM</span>.<span class="hljs-property">NOT_LOGIN</span>;<br>  <span class="hljs-keyword">if</span> (needAccess === <span class="hljs-variable constant_">ACCESS_ENUM</span>.<span class="hljs-property">NOT_LOGIN</span>) &#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>  &#125;<br>  <span class="hljs-comment">// 如果用户登录才能访问</span><br>  <span class="hljs-keyword">if</span> (needAccess === <span class="hljs-variable constant_">ACCESS_ENUM</span>.<span class="hljs-property">USER</span>) &#123;<br>    <span class="hljs-comment">// 如果用户没登录，那么表示无权限</span><br>    <span class="hljs-keyword">if</span> (loginUserAccess === <span class="hljs-variable constant_">ACCESS_ENUM</span>.<span class="hljs-property">NOT_LOGIN</span>) &#123;<br>      <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>    &#125;<br>  &#125;<br>  <span class="hljs-comment">// 如果需要管理员权限</span><br>  <span class="hljs-keyword">if</span> (needAccess === <span class="hljs-variable constant_">ACCESS_ENUM</span>.<span class="hljs-property">ADMIN</span>) &#123;<br>    <span class="hljs-comment">// 如果不为管理员，表示无权限</span><br>    <span class="hljs-keyword">if</span> (loginUserAccess !== <span class="hljs-variable constant_">ACCESS_ENUM</span>.<span class="hljs-property">ADMIN</span>) &#123;<br>      <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>    &#125;<br>  &#125;<br>  <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>&#125;;<br><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> checkAccess;<br><br></code></pre></td></tr></table></figure>

<p>使用计算属性，使得用户信息发生变更时，触发菜单栏的重新渲染，。</p>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// 展示在菜单上的路由数组</span><br><span class="hljs-keyword">const</span> visibleRoutes = <span class="hljs-title function_">computed</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>  <span class="hljs-keyword">return</span> routes.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">item, index</span>) =&gt;</span> &#123;<br>    <span class="hljs-comment">// 根据配置过滤菜单</span><br>    <span class="hljs-keyword">if</span> (item.<span class="hljs-property">meta</span>?.<span class="hljs-property">hideInmenu</span>) &#123;<br>      <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>    &#125;<br>    <span class="hljs-comment">//根据用户权限过滤菜单</span><br>    <span class="hljs-keyword">if</span> (<br>      !<span class="hljs-title function_">checkAccess</span>(store.<span class="hljs-property">state</span>.<span class="hljs-property">user</span>?.<span class="hljs-property">loginUser</span>, item.<span class="hljs-property">meta</span>?.<span class="hljs-property">access</span> <span class="hljs-keyword">as</span> <span class="hljs-built_in">string</span>)<br>    ) &#123;<br>      <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>    &#125;<br><br>    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>  &#125;);<br>&#125;);<br></code></pre></td></tr></table></figure>

<h2 id="全局项目入口"><a href="#全局项目入口" class="headerlink" title="全局项目入口"></a>全局项目入口</h2><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">const</span> <span class="hljs-title function_">doInit</span> = (<span class="hljs-params"></span>) =&gt; &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;项目全局入口&quot;</span>);<br>&#125;;<br><br><span class="hljs-title function_">onMounted</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>  <span class="hljs-title function_">doInit</span>();<br>&#125;);<br></code></pre></td></tr></table></figure>



<h2 id="根据后端生成接口"><a href="#根据后端生成接口" class="headerlink" title="根据后端生成接口"></a>根据后端生成接口</h2><p>后端接口文档：</p>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20240201113411843.png" alt="image-20240201113411843"></p>
<p>我们根据后端接口文档，一键生成前端 HTTP 请求接口：</p>
<p>官方文档：</p>
<p><a target="_blank" rel="noopener" href="https://github.com/ferdikoomen/openapi-typescript-codegen">ferdikoomen/openapi-typescript-codegen: NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification (github.com)</a></p>
<p>安装：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">npm install openapi-typescript-codegen --save-dev<br></code></pre></td></tr></table></figure>

<p>执行命令生成代码：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">openapi --input http://localhost:8121/api/v2/api-docs?group=memory-oj --output ./generated --client axios<br></code></pre></td></tr></table></figure>

<p><img src="C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20240201112520652.png" alt="image-20240201112520652"></p>
<p>如上，执行成功，成功生成 HTTP 请求接口</p>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20240117215206638.png" alt="image-20240117215206638"></p>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20240117215050598.png" alt="image-20240117215050598"></p>
<p><img src="https://gitee.com/deng-2022/pictures/raw/master/images/image-20240117215227942.png" alt="image-20240117215227942"></p>
<h2 id="MarkDown-编辑器"><a href="#MarkDown-编辑器" class="headerlink" title="MarkDown 编辑器"></a>MarkDown 编辑器</h2><h2 id="代码编辑器"><a href="#代码编辑器" class="headerlink" title="代码编辑器"></a>代码编辑器</h2>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/Vue-CLI/" class="category-chain-item">Vue CLI</a>
  
  
    <span>></span>
    
  <a href="/categories/Vue-CLI/Acro-Design-%E7%BB%84%E4%BB%B6%E5%BA%93/" class="category-chain-item">Acro Design 组件库</a>
  
  
    <span>></span>
    
  <a href="/categories/Vue-CLI/Acro-Design-%E7%BB%84%E4%BB%B6%E5%BA%93/%E7%BB%8F%E9%AA%8C/" class="category-chain-item">经验</a>
  
  

  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E7%BB%8F%E9%AA%8C/">#经验</a>
      
        <a href="/tags/Vue-CLI/">#Vue CLI</a>
      
        <a href="/tags/Acro-Design-%E7%BB%84%E4%BB%B6%E5%BA%93/">#Acro Design 组件库</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>破茧成蝶：从零开始构建独具匠心前端项目框架</div>
      <div>http://example.com/2024/01/17/破茧成蝶：从零开始构建独具匠心前端项目框架/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Memory</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2024年1月17日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2024年5月6日</div>
        </div>
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2024/01/17/%E7%B9%81%E6%98%9F%E7%82%B9%E7%82%B9%EF%BC%9A%E4%BB%8E%E9%9B%B6%E5%BC%80%E5%A7%8B%E6%9E%84%E5%BB%BA%E5%88%AB%E5%85%B7%E4%B8%80%E6%A0%BC%E7%9A%84%20Spring%20Boot%20%E9%A1%B9%E7%9B%AE%E6%A1%86%E6%9E%B6/" title="繁星点点：从零开始构建别具一格的 Spring Boot 项目框架">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">繁星点点：从零开始构建别具一格的 Spring Boot 项目框架</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2024/01/15/Memory%20OJ%20%E5%9C%A8%E7%BA%BF%E5%88%A4%E9%A2%98%E7%B3%BB%E7%BB%9F/" title="Memory OJ 在线判题系统">
                        <span class="hidden-mobile">Memory OJ 在线判题系统</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments">
      

    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.fluid-dev.com/docs/guide/" target="_blank" rel="nofollow noopener"> <span>Memory | 个人文档站点</span> </a>
<i class="iconfont icon-love"></i> <a href="https://hexo.fluid-dev.com/docs/guide/" target="_blank" rel="nofollow noopener"> <span>配置指南 | Hexo Fluid 用户手册</span> </a>   
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="leancloud-site-pv-container" style="display: none">
        总访问量 
        <span id="leancloud-site-pv"></span>
         次
      </span>
    
    
      <span id="leancloud-site-uv-container" style="display: none">
        总访客数 
        <span id="leancloud-site-uv"></span>
         人
      </span>
    
    

  
</div>

  
  
    <!-- 备案信息 ICP for China -->
    <div class="beian">
  <span>
    <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
      京ICP证123456号
    </a>
  </span>
  
    
      <span>
        <a
          href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=12345678"
          rel="nofollow noopener"
          class="beian-police"
          target="_blank"
        >
          
            <span style="visibility: hidden; width: 0">|</span>
            <img src="/img/police_beian.png" alt="police-icon"/>
          
          <span>京公网安备12345678号</span>
        </a>
      </span>
    
  
</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>







  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="/js/leancloud.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
